package com.example.howard.xiao7music.ui.Activity.LocalActivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.service.MusicPlayerService;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.MainFragmentAdapter;
import com.example.howard.xiao7music.ui.fragment.LocalFrament.LocalAllSongFragment;
import com.example.howard.xiao7music.ui.fragment.LocalFrament.LocalFolder;
import com.example.howard.xiao7music.ui.fragment.LocalFrament.LocalSinger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LocalSongActivity extends FragmentActivity implements View.OnClickListener,
        ViewPager.OnPageChangeListener{
    //退出延长时间
    private long exitTime = 0;
    private MyApplication myApplication;
    // 三个textview
    private TextView tab1Tv, tab2Tv,tab3Tv;
    // 指示器
    private ImageView cursorImg;
    private ViewPager viewPager;
    // fragment对象集合
    private ArrayList<Fragment> fragmentsList;
    // 记录当前选中的tab的index
    private int currentIndex = 0;
    // 指示器的偏移量
    private int offset = 0;
    // 左margin
    private int leftMargin = 0;
    // 屏幕宽度
    private int screenWidth = 0;
    // 屏幕宽度的三分之一
    private int screen1_3;
    //
    private RelativeLayout.LayoutParams lp;

    private BottomPlayer bottomplayer,player;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction,transaction_2;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_song);
        myApplication= (MyApplication) getApplication();
        bottomplayer=new BottomPlayer();
        init();
        fragmentManager=getFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.local_bottomplaybar,bottomplayer);
        transaction.commit();
    }



    @Override
    protected void onResume() {
        super.onResume();
//        player=myApplication.getBottomPlayer();
//        bottomplayer=new BottomPlayer();
//        bottomplayer=bottomplayer.newInstance(player);
//        bottomplayer=myApplication.getBottomPlayer();
//        myApplication.setBottomPlayer(bottomplayer);
//        fragmentManager=getFragmentManager();
//        transaction=fragmentManager.beginTransaction();
//        transaction.replace(R.id.local_bottomplaybar,bottomplayer);
//        transaction_2=fragmentManager.beginTransaction();
//        bottomplayer=myApplication.getBottomPlayer();
//        transaction.add(R.id.local_bottomplaybar,bottomplayer);

//        transaction.commit();
        getBottomPlayerState(bottomplayer);


    }

    @Override
    protected void onPause() {
        super.onPause();
//        transaction_2.remove(bottomplayer).commit();
        setBottomPlayerState(bottomplayer.getMusicPlayerService(),bottomplayer.getSongList(),
                bottomplayer.getMap(),bottomplayer.getPosition());
    }



    /**
     * 初始化操作
     */
    private void init() {

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screen1_3 = screenWidth / 3;

        cursorImg = (ImageView) findViewById(R.id.local_cursor);
        lp = (RelativeLayout.LayoutParams) cursorImg.getLayoutParams();
        leftMargin = lp.leftMargin;

        tab1Tv = (TextView) findViewById(R.id.local_tab1_tv);
        tab2Tv = (TextView) findViewById(R.id.local_tab2_tv);
        tab3Tv= (TextView) findViewById(R.id.local_tab3_tv);

        tab1Tv.setOnClickListener(this);
        tab2Tv.setOnClickListener(this);
        tab3Tv.setOnClickListener(this);

        initViewPager();
    }

    /**
     * 初始化viewpager
     */
    private void initViewPager() {
        viewPager = (ViewPager) findViewById(R.id.local_third_vp);
        fragmentsList = new ArrayList<>();
        Fragment fragment = new LocalAllSongFragment();
        fragmentsList.add(fragment);
        fragment = new LocalSinger();
        fragmentsList.add(fragment);
        fragment = new LocalFolder();
        fragmentsList.add(fragment);
        viewPager.setAdapter(new MainFragmentAdapter(getSupportFragmentManager(),
                fragmentsList));
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.local_tab1_tv:
                viewPager.setCurrentItem(0);
                break;
            case R.id.local_tab2_tv:
                viewPager.setCurrentItem(1);
                break;
            case  R.id.local_tab3_tv:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        offset = (screen1_3 - cursorImg.getLayoutParams().width) / 2;
        final float scale = getResources().getDisplayMetrics().density;
        if (position == 0) {
            lp.leftMargin = (int) (positionOffsetPixels / 3) + offset;
        }else if (position==1){
            lp.leftMargin=(int) (screen1_3)+(positionOffsetPixels / 3) + offset;
        }else {
            lp.leftMargin=(int) (2*screen1_3)+(positionOffsetPixels / 3) + offset;
        }
        cursorImg.setLayoutParams(lp);
        currentIndex = position;
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //获取bottomplayer的状态
    private void getBottomPlayerState (BottomPlayer player){
        if (myApplication.musicPlayerService!=null){
            player.setMusicPlayerService(myApplication.musicPlayerService);
        }
        if (myApplication.songdatalist.size()>0){
            player.setSongList(myApplication.songdatalist);
            player.setMap(myApplication.map);
            player.setPosition(myApplication.position);
        }

    }

    //上传bottomplayer的状态
    private void setBottomPlayerState (MusicPlayerService service, List<Map<String, Object>> songlist,
                                       Map<String, Object> map, int pos){
        myApplication.setMusicPlayerService(service);
        myApplication.setSongdatalist(songlist);
        myApplication.setMap(map);
        myApplication.setPosition(pos);
    }

    //获得bottomplayer实例


    public BottomPlayer getBottomplayer() {
        return bottomplayer;
    }
}
